package com.ai.networking.netty.serverDynamicsLink;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * 链路的异步连接
 * @Author jiaok
 * @Date 2024-05-13 16:33
 */
@Slf4j
public class AsyncQueue {

    private static final  int THREAD_SIZE = 100;
    private static final  int MAX_THREAD_SIZE = 10000;
    private static final int QUEUE_SIZE =1000;

    //线程池
    private static final ExecutorService connectAsync = new ThreadPoolExecutor(
            THREAD_SIZE, MAX_THREAD_SIZE,
            60L,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(QUEUE_SIZE),
            new ThreadFactory() {
                @Override
                public Thread newThread(Runnable r) {
                    Thread thread = new Thread(r);
                    thread.setName("netty_server_async_connect_thread ");
                    return thread;
                }
            },
            new RejectedExecutionHandler() {
                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    log.error("async connect is error rejected ,runnable:{} ,executor:{}",r,executor);
                }
            }
            );

    /**
     * 执行任务
     * @param runnable
     */
    public static void submit(Runnable runnable){
        connectAsync.submit(runnable);
    }

}
